package com.afkt.project.slice;


import com.afkt.project.camerakit.QRCodeScanActivity;
import com.afkt.project.model.item.ButtonList;
import com.afkt.project.model.item.ButtonValue;
import com.afkt.project.ui.adapter.ButtonAdapter;
import com.afkt.project.ui.slice.*;
import com.afkt.project.util.SkipUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.DevUtils;
import com.dev.utils.app.toast.ToastTintUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.bundle.IBundleManager;

public class UiSlice extends AbilitySlice {


    private DirectionalLayout bar_layout;

    private DirectionalLayout ui_bar;

    private Image ui_return;

    private ListContainer UI_list;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_bar_layout);
        ui_bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ui_bar, null, false);
        bar_layout.addComponent(ui_bar);
        ui_return = (Image) ui_bar.findComponentById(ResourceTable.Id_ui_return);
        ui_return.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
        UI_list = (ListContainer) findComponentById(ResourceTable.Id_UI_List);
        ButtonAdapter buttonAdapter = new ButtonAdapter(ButtonList.getModuleUIButtonValues(), this);
        UI_list.setItemProvider(buttonAdapter);
        UI_list.setItemClickedListener((listContainer, component, i, l) -> {
            ButtonValue buttonValue = (ButtonValue) UI_list.getItemProvider().getItem(i);
            switch (buttonValue.type) {
                case ButtonValue.BTN_TOAST_TINT: // ToastTint ( 着色美化 Toast )
                    SkipUtils.startActivity(new ToastTintSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_UI_EFFECT: // 常见 UI、GradientDrawable 效果等
                    SkipUtils.startActivity(new UIEffectActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_STATUS_BAR: // 点击 显示 / 隐藏 ( 状态栏 )
                     SkipUtils.startActivity(new StatusBarSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_TEXT_CALC: // 计算字体宽度、高度
                     SkipUtils.startActivity(new TextCalcSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_ADAPTER_EDITS: // Adapter Item EditText 输入监听
                    SkipUtils.startActivity(new AdapterEditsActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_MULTI_SELECT: // 多选辅助类 MultiSelectAssist
                    SkipUtils.startActivity(new MultiSelectActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_GPU_ACV: // GPU ACV 文件滤镜效果
                    //  SkipUtils.startActivity(GPUFilterACVActivity.class, buttonValue);
                    ToastTintUtils.error("该功能暂不支持");
                    break;
                case ButtonValue.BTN_GPU_FILTER: // GPU 滤镜效果
                    ToastTintUtils.error("该功能暂不支持");
                    //   SkipUtils.startActivity(GPUFilterActivity.class, buttonValue);
                    break;
                case ButtonValue.BTN_QRCODE_CREATE: // 创建二维码
                      SkipUtils.startActivity(new QRCodeCreateSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_QRCODE_IMAGE: // 二维码图片解析
                    SkipUtils.startActivity(new QRCodeImageSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_QRCODE_SCAN: // 二维码扫描解析
//                        SkipUtils.startActivity(new QRCodeScanActivity(), buttonValue);
                    SkipUtils.startNewAbility(UiSlice.this,"com.ohos.afkt","com.afkt.project.NewTaskQRAbility");
                    break;
                case ButtonValue.BTN_CAPTURE_PICTURE: // CapturePictureUtils 截图工具类
                    // SkipUtils.startActivity(CapturePictureActivity.class, buttonValue);
                    ToastTintUtils.error("该功能暂不支持");
                    break;
                case ButtonValue.BTN_TEXTVIEW: // 两个 TextView 显示效果
                     SkipUtils.startActivity(new TextViewSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_ITEM_STICKY: // RecyclerView 吸附效果
                    SkipUtils.startActivity(new HeaderListSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_RECY_ITEM_SLIDE: // RecyclerView 滑动删除、上下滑动
                    SkipUtils.startActivity(new SwipeActionSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_RECY_LINEAR_SNAP: // LinearSnapHelper - RecyclerView
                    SkipUtils.startActivity(new PagerSnapActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_RECY_LINEAR_SNAP_MAX: // LinearSnapHelper - 无限滑动
                    SkipUtils.startActivity(new PagerSnapMAXActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_RECY_PAGER_SNAP: // PagerSnapHelper - RecyclerView
                    SkipUtils.startActivity(new PagerSnapActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_RECY_PAGER_SNAP_MAX: // PagerSnapHelper - 无限滑动
                    SkipUtils.startActivity(new PagerSnapMAXActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_SHAPEABLE_IMAGE_VIEW: // Material ShapeableImageView
                    SkipUtils.startActivity(new ShapeableImageViewActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_BOTTOM_SHEET: // Material BottomSheet
                    SkipUtils.startActivity(new BottomSheetActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_BOTTOM_SHEET_DIALOG: // Material BottomSheetDialog
                    SkipUtils.startActivity(new BottomSheetDialogActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_PALETTE: // Palette 调色板
                    SkipUtils.startActivity(new PaletteActivity(), buttonValue);
                    break;
                case ButtonValue.BTN_FLEXBOX_LAYOUTMANAGER: // Flexbox LayoutManager
                    SkipUtils.startActivity(new FlexboxLayoutManagerSlice(), buttonValue);
                    break;
                case ButtonValue.BTN_CHIP: // Material Chip、ChipGroups、ChipDrawable
                    // SkipUtils.startActivity(ChipActivity.class, buttonValue);
                    ToastTintUtils.error("该功能暂不支持");
                    break;
                case ButtonValue.BTN_VIEWPAGER2: // ViewPager2
                    SkipUtils.startActivity(new ViewPager2Activity(), buttonValue);
                    break;

            }
        });
        //requestPermissions();

    }


    //申请权限
    private void requestPermissions() {
        if (verifySelfPermission("ohos.permission.CAMERA") != IBundleManager.PERMISSION_GRANTED) {
            // 应用未被授予权限
            if (canRequestPermission("ohos.permission.CAMERA")) {
                // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
                requestPermissionsFromUser(
                        new String[]{"ohos.permission.CAMERA"}, 0);
            } else {
                // 显示应用需要权限的理由，提示用户进入设置授权
                ToastTintUtils.success("该应用一定需要相机权限");
          /*      Intent intent = new Intent();
                Intent.OperationBuilder builder = new Intent.OperationBuilder();
                builder.withAction(IntentConstants.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                intent.setOperation(builder.build());
                intent.addFlags(Intent.FLAG_ABILITY_CONTINUATION);
                startAbilityForResult(intent, 1);*/
            }
        } else {
            // 权限已被授予
        }
    }

    @Override
    public void onActive() {
        super.onActive();
        // 如果标题消失， 重新添加。
        if (bar_layout.getChildCount() == 0) {
            bar_layout.addComponent(ui_bar);
        }
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(ui_bar);
        super.onBackground();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
